package com.syaccp.controller;

import com.github.pagehelper.PageInfo;
import com.syaccp.model.TOrderCategory;
import com.syaccp.model.TOrderDetail;
import com.syaccp.model.TOrderImage;
import com.syaccp.response.PicturePath;
import com.syaccp.service.TOrderCategoryService;
import com.syaccp.service.TOrderDetailService;
import com.syaccp.service.TOrderImageService;
import com.syaccp.utils.AliOSSUtils;
import com.syaccp.utils.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chen
 * @since 2023-07-10
 */
@RestController
@RequestMapping("/api/orderImage")
@CrossOrigin
public class OrderImageController {

    @Resource
    private AliOSSUtils aliOSSUtils;

    @Resource
    TOrderImageService iOrderImageService;

    @Resource
    private TOrderCategoryService orderCategoryService;

    @Resource
    private TOrderDetailService iOrderDetailService;

    @ApiOperation(value = "查询任务图片信息")
    @ApiImplicitParam()
    @GetMapping("/QueryDetailPicture")
    public Result QueryAllDetailMsg(Integer id){
        System.out.println("id = " + id);
        Result result=new Result();
        List<TOrderImage> orderImages = iOrderImageService.QueryDetailImage(id);

        if (orderImages!=null){
            //查询成功
            result.setCode(200);
            for (TOrderImage orderImage1 : orderImages) {
                    //设置图片路径
                    String picture_get_path = PicturePath.GET_PICTURE_GET_PATH(orderImage1.getImage());
                    orderImage1.setImage(picture_get_path);
            }
            result.setData(orderImages);
            return  result;

        }else {
            //查询失败
            result.setCode(500);
        }
        return result;
    }


    @PostMapping("/handleUploadImg")
    @ApiOperation(value = "上传图片",notes = "上传主图")
    @ApiImplicitParam(name = "file", value = "file对象")
    public Result handleUploadImg(MultipartFile file){

        System.out.println("=========="+1);
        String upload = null;
        try {
            upload  = aliOSSUtils.upload(file);



        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("=========="+upload);
        return  Result.success(upload);
    }


    @GetMapping("/upClick")
    @ApiOperation(value = "主图信息",notes = "保存主图信息")
    @ApiImplicitParam(name = "image", value = "image对象")
    public Result upClick( TOrderImage orderImage){
        System.out.println("-----------1111"+orderImage.getSceneName());

        TOrderDetail orderDetail = new TOrderDetail();

        String sceneName = orderImage.getSceneName();
        String[] split = sceneName.split(",");
        orderDetail.setSceneId(Integer.valueOf(split[0]));

        TOrderCategory orderCategory = iOrderImageService.selectOneById(orderDetail.getSceneId());

        orderDetail.setSceneName(orderCategory.getName());
        orderDetail.setImage(orderImage.getImage());

        iOrderDetailService.updateOrderDetail(orderDetail,orderImage.getTaskId());


        Integer integer = iOrderImageService.addImage(orderImage);

        if (integer!=null&&integer==1){
            return Result.success("成功!");
        }


        return  Result.success("失败!");
    }




    @PostMapping("/uploadPictures")
    @ApiOperation(value = "赠图",notes = "保存赠图信息")
    @ApiImplicitParam(name = "image", value = "image对象")
    public Result uploadPictures(@RequestParam("files") MultipartFile []file
            ,@RequestParam("taskId") Integer taskId,@RequestParam("picType") Integer picType) throws IOException {
        TOrderImage orderImage = new TOrderImage();
        Integer integer = null;
        String up = "";
        for (int i = 0; i <file.length ; i++) {
            String upload = aliOSSUtils.upload(file[i]);

            if (i!=0){
                up+=",";
            }

            up+=upload;

            TOrderDetail orderDetail = new TOrderDetail();
            orderDetail.setAlbumPics(up);


            iOrderDetailService.updateOrderDetail(orderDetail,taskId);

            orderImage.setTaskId(taskId);
            orderImage.setPicType(picType);
            orderImage.setImage(upload);
            integer= iOrderImageService.addImage(orderImage);
        }





        return Result.success(integer);
    }

}
